import sys
import pandas as pd
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QFormLayout, QLabel, QComboBox, QPushButton, QTextEdit, QSpinBox

class ThermalComfortFinder(QWidget):
    def __init__(self):
        super().__init__()
        self.load_data()
        self.initUI()

    def load_data(self):
        # 从Excel文件中读取数据
        self.df_results = pd.read_excel('bathroom_comfort_analysis.xlsx')

    def initUI(self):
        self.setWindowTitle('Thermal Comfort Finder')
        self.setGeometry(100, 100, 600, 500)

        layout = QVBoxLayout()
        form_layout = QFormLayout()

        # 人群类型下拉框
        self.group_combo = QComboBox()
        self.group_combo.addItems(self.df_results['Group'].unique())
        form_layout.addRow(QLabel('选择人群:'), self.group_combo)

        # 温度范围选择
        form_layout.addRow(QLabel('温度范围 (°C):'))
        self.temp_min = QSpinBox()
        self.temp_min.setRange(25, 40)
        self.temp_min.setValue(25)
        self.temp_max = QSpinBox()
        self.temp_max.setRange(25, 40)
        self.temp_max.setValue(40)
        form_layout.addRow(QLabel('最低温度:'), self.temp_min)
        form_layout.addRow(QLabel('最高温度:'), self.temp_max)

        # 湿度范围选择
        form_layout.addRow(QLabel('湿度范围 (% RH):'))
        self.humidity_min = QSpinBox()
        self.humidity_min.setRange(40, 80)
        self.humidity_min.setValue(40)
        self.humidity_max = QSpinBox()
        self.humidity_max.setRange(40, 80)
        self.humidity_max.setValue(80)
        form_layout.addRow(QLabel('最低湿度:'), self.humidity_min)
        form_layout.addRow(QLabel('最高湿度:'), self.humidity_max)

        # 风速选择下拉框
        self.air_speed_combo = QComboBox()
        self.air_speed_combo.addItems([f"{i/10:.1f}" for i in range(1, 5)])  # 从0.1到0.4 m/s的风速选项
        form_layout.addRow(QLabel('选择风速 (m/s):'), self.air_speed_combo)

        # 计算按钮
        self.calc_button = QPushButton('查找最佳的温湿度条件')
        self.calc_button.clicked.connect(self.findBestCondition)
        form_layout.addWidget(self.calc_button)

        # 结果显示区
        self.result_text = QTextEdit()
        self.result_text.setReadOnly(True)
        layout.addLayout(form_layout)
        layout.addWidget(self.result_text)

        self.setLayout(layout)

    def findBestCondition(self):
        # 获取用户输入
        group = self.group_combo.currentText()
        air_speed = float(self.air_speed_combo.currentText())  # 选中的风速
        temp_min = self.temp_min.value()
        temp_max = self.temp_max.value()
        humidity_min = self.humidity_min.value()
        humidity_max = self.humidity_max.value()

        # 基础新陈代谢率
        metabolic_rates = {
            '女': 1.5,
            '男': 2.0,
            '儿童': 1.6,
            '老年人': 1.4,
            '孕妇': 1.7,
            '残疾人': 1.4
        }

        # 选择的群体基础新陈代谢率
        base_met = metabolic_rates.get(group, 1.5)  # 默认为1.5

        # 筛选数据
        filtered_df = self.df_results[
            (self.df_results['Group'] == group) &
            (self.df_results['Air Speed (m/s)'] == air_speed) &
            (self.df_results['Temperature (°C)'] >= temp_min) &
            (self.df_results['Temperature (°C)'] <= temp_max) &
            (self.df_results['Humidity (%)'] >= humidity_min) &
            (self.df_results['Humidity (%)'] <= humidity_max)
        ]

        # 查找PMV最接近0的条件
        if not filtered_df.empty:
            best_condition = filtered_df.loc[(filtered_df['PMV']).abs().idxmin()]

            # 显示结果
            result_text = (
                f"最适合的温度: {best_condition['Temperature (°C)']} °C\n"
                f"最适合的湿度: {best_condition['Humidity (%)']} % RH\n"
                f"最适合的风速: {best_condition['Air Speed (m/s)']} m/s\n"
                f"新陈代谢率: {best_condition['Metabolic Rate (met)']}\n"
                f"PMV: {best_condition['PMV']:.2f}\n"
                f"PPD (%): {best_condition['PPD (%)']:.2f}\n"
            )
        else:
            result_text = "没有符合条件的记录。"

        self.result_text.setText(result_text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    finder = ThermalComfortFinder()
    finder.show()
    sys.exit(app.exec_())
